﻿using System;
using System.Buffers;
using System.Text;
using Xunit;

namespace Unity.String.Tests
{
    public class Utf8FormatHelperTest
    {
        class FixedBufferWriter<T> : IBufferWriter<T>
        {
            private int capacity;
            private T[] buffer;
            private int index;
            
            public FixedBufferWriter(int capacity)
            {
                this.capacity = capacity;
                if (this.capacity > 0)
                {
                    buffer = new T[capacity];
                }
                else
                {
                    buffer = Array.Empty<T>();
                }
                index = 0;
            }

            public void Advance(int count)
            {
                //if (count < 0)
                //    throw new ArgumentException(nameof(count));

                //if (this.index > this.buffer.Length - count)
                //    throw new InvalidOperationException("Capacity to small to get advice");

                //this.index += count;
            }

            public Memory<T> GetMemory(int sizeHint = 0)
            {
                return buffer.AsMemory(index);
            }

            public Span<T> GetSpan(int sizeHint = 0)
            {
                return buffer.AsSpan(index);
            }
        }

        [Fact]
        public void MethodsTest()
        {
            {
                int testLeftAlignment = -128;
                int testRightAlignment = 128;

                string testStringValue = "Test";
                long testLongValue = 123456L;
                DayOfWeek testDayOfWeekValue = DayOfWeek.Monday;
                
                StandardFormat testFormat = StandardFormat.Parse("G");
                
                IBufferWriter<byte> testBufferWriter = new ArrayBufferWriter<byte>(1024);
                
                
                
                Utf8FormatHelper.FormatTo<IBufferWriter<byte>, string>(ref testBufferWriter, 
                    testStringValue, testLeftAlignment, testFormat, nameof(testStringValue));
                Utf8FormatHelper.FormatTo<IBufferWriter<byte>, string>(ref testBufferWriter, 
                    testStringValue, testRightAlignment, testFormat, nameof(testStringValue));
                
                //testBufferWriter = new ArrayBufferWriter<byte>(1024);
                
                Utf8FormatHelper.FormatTo<IBufferWriter<byte>, long>(ref testBufferWriter,
                    testLongValue, testLeftAlignment, testFormat, nameof(testStringValue));
                Utf8FormatHelper.FormatTo<IBufferWriter<byte>, long>(ref testBufferWriter,
                    testLongValue, testRightAlignment, testFormat, nameof(testStringValue));

                //testBufferWriter = new ArrayBufferWriter<byte>(1024);
                
                Utf8FormatHelper.FormatTo<IBufferWriter<byte>, DayOfWeek>(ref testBufferWriter,
                    testDayOfWeekValue, testLeftAlignment, testFormat, nameof(testStringValue));
                Utf8FormatHelper.FormatTo<IBufferWriter<byte>, DayOfWeek>(ref testBufferWriter,
                    testDayOfWeekValue, testRightAlignment, testFormat, nameof(testStringValue));

                // IBufferWriter<byte> testFixedCapacityBufferWriter =
                //     new FixedBufferWriter<byte>(0);
                // Utf8FormatHelper.FormatTo<IBufferWriter<byte>, string>(ref testFixedCapacityBufferWriter, 
                //     testStringValue, testLeftAlignment, testFormat, nameof(testStringValue));
                // Utf8FormatHelper.FormatTo<IBufferWriter<byte>, string>(ref testFixedCapacityBufferWriter, 
                //     testStringValue, testRightAlignment, testFormat, nameof(testStringValue));
                
            }
            
        }
    }
}